//
//  SYBaseViewController.swift
//  SingWeibo
//
//  Created by shiying on 15/12/15.
//  Copyright © 2015年 shiying. All rights reserved.
//

import UIKit

class SYBaseViewController: UITableViewController {

    //判断是否登录
    var userLogin = SYUseAccount.userLogin
    //如果没
    override func loadView() {
        userLogin ? super.loadView() : setupVistorView()
    }
    
    //设置访客视图 
    func setupVistorView() {
        //设置一个View
        
        view = vistorView
        
        vistorView.delegate = self
        
        /// 设置全局导航栏
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action: "registerClick")
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.Plain, target: self, action: "loginClick")

        
        /// 设置访客视图
        if self is SYHomeViewController {
            vistorView.stratAnimation()
            
            // 退出后台暂停
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "didEnterBackground", name:
                UIApplicationDidEnterBackgroundNotification, object: nil)
            
            // 进入程序继续
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "didBecomeActive", name: UIApplicationDidBecomeActiveNotification, object: nil)

        } else if self is SYMessageViewController {
            vistorView.setupVistorInfo("visitordiscover_image_message", messageLabel: "登录后，别人评论你的微博，发给你的消息，都会在这里收到通知")
        } else if self is SYDiscoveryViewController {
            vistorView.setupVistorInfo("visitordiscover_image_message", messageLabel: "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过")
        } else if self is SYProfileViewController {
            vistorView.setupVistorInfo("visitordiscover_image_profile", messageLabel: "登录后，你的微博、相册、个人资料会显示在这里，展示给别人")
        }
    }
    
    //MARK: - 全局导航栏的方法
    func registerClick() {
        print("注册")
    }
    
    func loginClick() {
        let oAuthVC = SYOauthViewController()
        
        presentViewController(UINavigationController(rootViewController: oAuthVC), animated: true, completion: nil)
    }
    
    
    //MARK: - 移除通知
    deinit {
        if self is SYHomeViewController {
            NSNotificationCenter.defaultCenter().removeObserver(self)
        }
    }
    
    //MARK: - 懒加载
    private lazy var vistorView: SYVistorView = SYVistorView()
    
    //MARK: - 通知方法
    func didEnterBackground() {
        vistorView.pauseAnimation()
    }
    
    func didBecomeActive() {
        vistorView.resumeAnimation()
    }
    
}

extension SYBaseViewController: SYVistorViewDelegate {
    /// 登录按钮
    func vistorViewLoginClick() {
        let oAuthVC = SYOauthViewController()
        
        presentViewController(UINavigationController(rootViewController: oAuthVC), animated: true, completion: nil)
    }
    
    /// 取消登录
    func vistorViewRegisterClick() {
        dismissViewControllerAnimated(true, completion: nil)
    }


}
